package com.buybuyyi.stylistcrm.order.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.buybuyyi.stylistcrm.order.dao.ConsigneeDao;
import com.buybuyyi.stylistcrm.order.dao.TransferOrderDao;
import com.buybuyyi.stylistcrm.order.dao.TransferOrderGoodsDao;
import com.buybuyyi.stylistcrm.order.helper.ConsigneeHelper;
import com.buybuyyi.stylistcrm.order.helper.OrderGoodsHelper;
import com.buybuyyi.stylistcrm.order.po.Consignee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.buybuyyi.stylistcrm.consts.DateFormatPattern;
import com.buybuyyi.stylistcrm.order.po.TransferOrder;
import com.buybuyyi.stylistcrm.order.po.TransferOrderGoods;
import com.buybuyyi.stylistcrm.utils.DateUtil;
import stylistcrm.core.query.ResultPagerModel;
import stylistcrm.core.services.order.dto.OrderGoodsDto;
import stylistcrm.core.services.order.dto.QueryOrderConditionDto;
import stylistcrm.core.services.order.dto.TransferOrderDto;
import stylistcrm.core.services.order.enums.EnumExceptionCode;
import stylistcrm.core.services.order.exception.BizException;
import stylistcrm.core.services.order.service.TransferOrderService;

/**
 * Created by cheny on 2016/8/25.
 */
@Service
public class TransferOrderServiceImpl implements TransferOrderService {

    @Autowired
    private TransferOrderDao transferOrderDao;
    
    @Autowired
    private TransferOrderGoodsDao transferOrderGoodsDao;

	@Autowired
	private ConsigneeDao consigneeDao;

    /**
     * 创建调货单
     * @param transferOrder
     * @return
     */
    @Override
    public Boolean createTransferOrder(TransferOrderDto transferOrder) throws BizException{
    	if(null == transferOrder){
    		throw new BizException(EnumExceptionCode.EC0001);
    	}
		Consignee consignee= ConsigneeHelper.getOrderConsignee(transferOrder);
		if(null==consignee){
			throw new BizException(EnumExceptionCode.EC0002);
		}
		if(consignee.getId()!=null){
			consigneeDao.updateByPrimaryKeySelective(consignee);
		}else{
			Integer consigneeId=consigneeDao.insertSelective(consignee);
			transferOrder.setConsigneeId(consigneeId);
		}
		OrderGoodsHelper.setGoodsList(transferOrder);
    	TransferOrder order=convertDto2Entity(transferOrder);
    	if(null!=order){
    		Integer ret=transferOrderDao.insertSelective(order);
        	if(ret<=0){
        		throw new BizException(EnumExceptionCode.EC0008);
        	}
			List<OrderGoodsDto> goodsList=transferOrder.getGoods();
			if(null == goodsList || goodsList.isEmpty()){
				throw new BizException(EnumExceptionCode.EC0009);
			}
			for(OrderGoodsDto goods:goodsList){
				TransferOrderGoods orderGoods=convertDto2Entity(goods);
				if(null==orderGoods){
					continue;
				}
				orderGoods.setTransferOrderId(order.getId());
				transferOrderGoodsDao.insertSelective(orderGoods);
			}
    	}
        return Boolean.TRUE;
    }

    /**
     * 
     * @描述：将进货单dto转换为entity
     * @param transferOrderDto
     * @return
     * @return PurchaseOrder
     * @exception
     * @createTime：2016年8月31日
     * @author: chenyong
     */
    private TransferOrder convertDto2Entity(TransferOrderDto transferOrderDto){
    	if(null == transferOrderDto){
    		return null;
    	}
    	TransferOrder order=new TransferOrder();
    	order.setStylistId(transferOrderDto.getStylistId());
    	order.setBelongToId(transferOrderDto.getBelongToId());
    	order.setAuditStatus(transferOrderDto.getAuditStatus());
    	order.setDescription(transferOrderDto.getDescription());
    	order.setModifierId(transferOrderDto.getModifierId());
    	if(null!=transferOrderDto.getModifyTime()){
    		order.setModifyTime(DateUtil.parseDate(transferOrderDto.getModifyTime(), DateFormatPattern.PATTERN_YYYY_MM_DD_HHMMSS));
    	}
    	order.setOrderStatus(transferOrderDto.getOrderStatus());
    	if(null!=transferOrderDto.getOrderTime()){
    		order.setOrderTime(DateUtil.parseDate(transferOrderDto.getOrderTime(),DateFormatPattern.PATTERN_YYYY_MM_DD_HHMMSS));
    	}else{
    		order.setOrderTime(new Date());
    	}
		order.setConsigneeId(transferOrderDto.getConsigneeId());
    	return order;
    }
    /**
     * 
     * @描述：将进货单商品dto转换为entity
     * @param goodsDto
     * @return
     * @return PurchaseOrderGoods
     * @exception
     * @createTime：2016年8月31日
     * @author: chenyong
     */
    private TransferOrderGoods convertDto2Entity(OrderGoodsDto goodsDto){
    	if(null==goodsDto){
    		return null;
    	}
    	TransferOrderGoods orderGoods=new TransferOrderGoods();
    	orderGoods.setColor(goodsDto.getColor());
    	orderGoods.setCount(goodsDto.getCount());
    	orderGoods.setCreateTime(new Date());
    	orderGoods.setDeliveredCount(goodsDto.getDeliveredCount());
    	orderGoods.setDesignerId(goodsDto.getDesignerId());
    	orderGoods.setGoodsBuyingPrice(goodsDto.getGoodsBuyingPrice());
    	orderGoods.setGoodsId(goodsDto.getGoodsId());
    	orderGoods.setGoodsName(goodsDto.getGoodsName());
    	orderGoods.setGoodsSalePrice(goodsDto.getGoodsSalePrice());
    	orderGoods.setGoodsTagPrice(goodsDto.getGoodsTagPrice());
    	orderGoods.setSize(goodsDto.getSize());
    	orderGoods.setYardageId(goodsDto.getYardageId());
    	return orderGoods;
    }
    /**
     * 调货单分页查询
     */
	@Override
	public ResultPagerModel<TransferOrderDto> getPageList(QueryOrderConditionDto queryCondition) {
		Integer currentPage=queryCondition.getCurrentPage();
		Integer pageSize=queryCondition.getPageSize();
		if(currentPage==null){
			currentPage=1;
			queryCondition.setCurrentPage(currentPage);
		}
		if(pageSize==null){
			pageSize=15;
			queryCondition.setPageSize(pageSize);
		}
		queryCondition.setStart((currentPage-1)*pageSize);
		List<Integer> ids=this.getPageIdList(queryCondition);
		ResultPagerModel<TransferOrderDto> resultPager=new ResultPagerModel<TransferOrderDto>();
		resultPager.setCurrentPage(currentPage);
		resultPager.setPageSize(pageSize);
		List<TransferOrderDto> transferOrderList=null;
		if(null==ids || ids.isEmpty()){
			transferOrderList=new ArrayList<>();
			resultPager.setTotalServerItems(0);
		}else{
			queryCondition.setIds(ids);
			transferOrderList=transferOrderDao.getPageList(queryCondition);
			Integer totalCount=this.getTotalCount(queryCondition);
			resultPager.setTotalServerItems(totalCount);
		}
		resultPager.setData(transferOrderList);
		return resultPager;
	}

	/**
	 * 通过主键查询调货单
	 * @param id
	 * @return
     */
	@Override
	public TransferOrderDto selectByPrimaryKey(Integer id) {
		QueryOrderConditionDto queryCondition=new QueryOrderConditionDto();
		List<Integer> ids=new ArrayList<Integer>();
		ids.add(id);
		queryCondition.setIds(ids);
		List<TransferOrderDto> transferOrderList=transferOrderDao.getPageList(queryCondition);
		if(null!=transferOrderList&&0!=transferOrderList.size()){
			return transferOrderList.get(0);
		}
		return null;
	}

	/**
	 * 根据条件查询主键id
	 * @param queryCondition
	 * @return
     */
	@Override
	public List<Integer> getPageIdList(QueryOrderConditionDto queryCondition) {
		return transferOrderDao.getPageIdList(queryCondition);
	}

	/**
	 * 查询总的记录数
	 * @param queryCondition
	 * @return
     */
	@Override
	public Integer getTotalCount(QueryOrderConditionDto queryCondition) {
		return transferOrderDao.getTotalCount(queryCondition);
	}

	/**
	 * 获取未完成的调货单
	 * @return
     */
	@Override
	public List<OrderGoodsDto> getGoodsList(Long userId) {
		return transferOrderGoodsDao.getGoodsList(userId);
	}
}
